package com.hand.todo.api.controller.v1;


import com.hand.todo.app.service.SoHeaderService;
import com.hand.todo.domain.repository.SoHeaderRepository;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.hzero.boot.scheduler.infra.annotation.JobHandler;
import org.hzero.boot.scheduler.infra.enums.ReturnT;
import org.hzero.boot.scheduler.infra.handler.IJobHandler;
import org.hzero.boot.scheduler.infra.tool.SchedulerTool;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


@JobHandler("PO_33069")
public class PoOrderJob implements IJobHandler {

    @Autowired
    private final SoHeaderService soHeaderService;
    @Autowired
    private final SoHeaderRepository soHeaderRepository;

    public PoOrderJob(SoHeaderService soHeaderService,
                      SoHeaderRepository soHeaderRepository) {
        this.soHeaderRepository = soHeaderRepository;
        this.soHeaderService = soHeaderService;
    }

    /**
     * 每天凌晨3点 ，定时将销售订单状态为已审批的修改为已关闭
     *
     * @param map
     * @param tool
     * @return
     */
    @Override
    public ReturnT execute(Map<String, String> map, SchedulerTool tool) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {}
        tool.info("调度任务执行");
        //刷新任务进度及描述
        tool.updateProgress(100, "任务执行中");
        //定时将销售订单状态为已审批的修改为已关闭
        String ORISTATUS = "SUBMITTED";
        String NEWSTATUS = "CLOSED";
        soHeaderService.updateStatus(ORISTATUS, NEWSTATUS);
        //修改完成
        tool.info("订单状态修改成功");
        return ReturnT.SUCCESS;
    }

    @Override
    public void onCreate(SchedulerTool tool) {
        tool.info("在执行任务前初始化数据！");
    }

    @Override
    public void onException(SchedulerTool tool) {
        tool.info("出错了！");
    }

    @Override
    public void onFinish(SchedulerTool tool, ReturnT returnT) {
        tool.info("结束了！");
    }
}